//
//  RecommandSegViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/8.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "RecommandHeadView.h"
#import "TT_BaseNaviView.h"
#import "UserBalanceInfoModel.h"

#import "XLPageViewController.h"
#import "MyRecommandViewController.h"
#import "SubsidyListViewController.h"

#import "RecommandSegViewController.h"

@interface RecommandSegViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce>

@property (nonatomic, strong) TT_BaseNaviView *naviView;

@property (nonatomic, strong) RecommandHeadView *headView;

@property (nonatomic, strong) UserBalanceInfoModel *headModel;


@property (nonatomic, strong) XLPageViewController *pageViewController;

@end

@implementation RecommandSegViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_balance];
}

- (void)initView {
    
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    self.view.backgroundColor = UIColorHex(0xffffff);
    
    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(120 + NavBar_Height + kStatusBarHeight);
    }];
    
    self.headView.start_y.constant = 20 + NavBar_Height + kStatusBarHeight;
    
    [self.view addSubview:self.naviView];
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(NavBar_Height + kStatusBarHeight);

    }];
    
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {

    switch (index) {
        case 0:{
            MyRecommandViewController *vc = [[MyRecommandViewController alloc] init];
            return vc;
        } break;
        case 1:{
            SubsidyListViewController *vc = [[SubsidyListViewController alloc] init];
            return vc;
        } break;
        default:
            break;
    }
    MyRecommandViewController *vc = [[MyRecommandViewController alloc] init];
    return vc;
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return [self titles][index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return [self titles].count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    NSLog(@"切换到了：%@",[self titles][index]);
}

#pragma mark -
#pragma mark 标题数据
- (NSArray *)titles {
    return @[@"我推荐的客户",@"补贴明细"];
}


- (RecommandHeadView *)headView {
    if (!_headView) {
        _headView = [[NSBundle mainBundle] loadNibNamed:@"RecommandHeadView"owner:nil options:nil].firstObject;
    }
    return _headView;
}

- (XLPageViewController *)pageViewController {
    if (!_pageViewController) {
        XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
        config.shadowLineColor = UIColorHex(0xFF4242);
        config.titleSelectedColor = UIColorHex(0xFF4242);
        config.titleSelectedFont = [UIFont systemFontOfSize:14];
        config.titleNormalFont = [UIFont systemFontOfSize:14];
        config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
        config.showTitleInNavigationBar = false;
        config.titleViewStyle = XLPageTitleViewStyleBasic;
        config.titleWidth = (TTScreenWidth)/2.5;
        config.titleViewHeight = 40;
        config.separatorLineHidden = true;
        config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
        config.shadowLineWidth = 20;
        _pageViewController = [[XLPageViewController alloc] initWithConfig:config];
    }
    return _pageViewController;
}

- (TT_BaseNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"TT_BaseNaviView"owner:nil options:nil].firstObject;
        _naviView.titleLab.text = @"推广补贴";
        _naviView.titleLab.textColor = UIColorHex(0xffffff);
        [_naviView.backBtn setImage:[UIImage imageNamed:@"public_back_white"] forState:UIControlStateNormal];
        [_naviView.backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _naviView;
}

- (void)request_balance {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_RecOrder];
    
    NSMutableDictionary *dic = @{}.mutableCopy;
//    NSDictionary *t = @{}.mutableCopy;
//    [t setValue:self.userId forKey:@"recommendedUserId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];

//    [dic setValue:t forKey:@"t"];
//    [dic setValue:@(1) forKey:@"pageNum"];
//    [dic setValue:@(20) forKey:@"pageSize"];

    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                weakSelf.headModel = [UserBalanceInfoModel modelWithDictionary:resultDic];
                weakSelf.headView.freezeLab.text =[NSString stringWithFormat:@"冻结中金额¥%@",[Utilities changeDoubleToString:weakSelf.headModel.iceAmount]];
                weakSelf.headView.totalLab.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:weakSelf.headModel.totalAmount]];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
